﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Web.Profile;

namespace WebSite.admin.member
{
    public partial class member_account_add : BaseAdmin
    {


        // Methods
        private void Bind()
        {
            string sql = "select * from DB_Pay where flag=1";
            DataTable table = new DataTable();
            table = SQLAccess.GreatDs(sql).Tables[0];
            if (table.Rows.Count > 0)
            {
                foreach (DataRow row in table.Rows)
                {
                    this.pay.Items.Add(new ListItem(row["payname"].ToString(), row["payid"].ToString()));
                }
            }
        }

        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            string sql = "select * from tbMember where email='" + re.replace(this.memberlogin.Value) + "'";
            DataTable table = new DataTable();
            table = SQLAccess.GreatDs(sql).Tables[0];
            if (table.Rows.Count > 0)
            {
                if (base.Request.QueryString["aid"] != null)
                {
                    SQLAccess.DoSql(string.Concat(new object[] { 
                    "update DB_Accountuser set memberid=", table.Rows[0]["memberid"].ToString(), ",price1=", re.replace(this.price1.Value), ",pay=", this.pay.SelectedValue, ",atype='", re.replace(this.atype.SelectedValue), "',usercontent='", re.replace(this.usercontent.Text), "',membercontent='", re.replace(this.membercontent.Text), "',states=", re.replace(this.states.SelectedValue), ",updatetime='", DateTime.Now, 
                    "',username='", this.Session["Adminlogin"], "' where auserid=", base.Request.QueryString["aid"], ""
                 }));
                    base.Response.Write("<script language='javascript'>alert('修改成功！');window.location.href='member_account.aspx';</script>");
                    base.Response.End();
                }
                else
                {
                    SQLAccess.DoSql(string.Concat(new object[] { 
                    "insert into DB_Accountuser (memberid,price1,pay,atype,usercontent,membercontent,states,updatetime,username) values (", table.Rows[0]["memberid"].ToString(), ",", re.replace(this.price1.Value), ",", this.pay.SelectedValue, ",'", re.replace(this.atype.SelectedValue), "','", re.replace(this.usercontent.Text), "','", re.replace(this.membercontent.Text), "',", re.replace(this.states.SelectedValue), ",'", DateTime.Now, 
                    "','", this.Session["Adminlogin"], "')"
                 }));
                    base.Response.Write("<script language='javascript'>alert('添加成功！');window.location.href='member_account.aspx';</script>");
                    base.Response.End();
                }
            }
            else
            {
                base.Response.Write("<script language='javascript'>alert('对不起，用户不存在！');window.location.href='member_account_add.aspx';</script>");
                base.Response.End();
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                this.Bind();
                if (base.Request.QueryString["aid"] != null)
                {
                    this.upbind();
                }
            }
        }

        private void upbind()
        {
            string sql = "select * from DB_accountuser where auserid=" + base.Request.QueryString["aid"] + "";
            DataTable table = new DataTable();
            table = SQLAccess.GreatDs(sql).Tables[0];
            if (table.Rows.Count > 0)
            {
                sql = "select * from tbMember where memberid=" + table.Rows[0]["memberid"].ToString() + " and isnull(flag,0)!=2 ";
                DataTable table2 = new DataTable();
                table2 = SQLAccess.GreatDs(sql).Tables[0];
                if (table2.Rows.Count > 0)
                {
                    this.memberlogin.Value = table2.Rows[0]["email"].ToString();
                }
                this.price1.Value = table.Rows[0]["price1"].ToString();
                this.atype.SelectedValue = table.Rows[0]["atype"].ToString();
                this.usercontent.Text = table.Rows[0]["usercontent"].ToString();
                this.membercontent.Text = table.Rows[0]["membercontent"].ToString();
                this.pay.SelectedValue = table.Rows[0]["pay"].ToString();
                this.states.SelectedValue = table.Rows[0]["states"].ToString();
            }
        }

    

    }
}